
<!DOCTYPE html>
<html lang="">


<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content>
  

  
    <meta name="description" content="python之kubernetes api 使用">
  
  
  
  <link rel="icon" type="image/x-icon" href="/images/footer-logo.png">
  
  <title>python之kubernetes api 使用 [ 51AIOps 专注于运维自动化  微信： kaipython ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="//cdn.bootcss.com/pure/1.0.0/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
</head>


<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    
    <span class="title" style="text-transform:none">51AIOps 专注于运维自动化  微信： kaipython</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            
              <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
            
          
      
  </ul>
   
</nav>

  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner" style='margin-left:-68px!important'>
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        python之kubernetes api 使用
      </h1>
      <span>
        
        <time class="time" datetime="2020-08-27T09:39:13.540Z">
        2020-08-27
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
    </header>

    <div class="post-content">
      <p>一、安装</p>
<blockquote>
<p>github：<a href="https://github.com/kubernetes-client/python" target="_blank" rel="noopener">https://github.com/kubernetes-client/python</a></p>
</blockquote>
<p>安装</p>
<blockquote>
<p>pip install kubernetes</p>
</blockquote>
<p>二、认证<br>1、kubeconfig文件认证<br>首先引入SDK支持库。然后将 ~/.kube 的config文件的内容复制到本地目录，保存为文件kubeconfig.yaml，然后运行下面的python代码</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">[root@k8s-m ~]<span class="comment"># cp .kube/config    kubeconfig.yaml</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#使用</span></span><br><span class="line"><span class="keyword">from</span> kubernetes <span class="keyword">import</span> client, config</span><br><span class="line">config.kube_config.load_kube_config(config_file=<span class="string">"/root/kubeconfig.yaml"</span>)</span><br></pre></td></tr></table></figure>

<p>三、api使用<br>1、列出资源信息</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">from</span> kubernetes <span class="keyword">import</span> client, config</span><br><span class="line">config.kube_config.load_kube_config(config_file=<span class="string">"/root/kubeconfig.yaml"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#获取API的CoreV1Api版本对象</span></span><br><span class="line">v1 = client.CoreV1Api()</span><br><span class="line"></span><br><span class="line"><span class="comment">#列出 namespaces</span></span><br><span class="line"><span class="keyword">for</span> ns <span class="keyword">in</span> v1.list_namespace().items:</span><br><span class="line">    print(ns.metadata.name)</span><br><span class="line">    </span><br><span class="line"><span class="comment">#列出所有的services</span></span><br><span class="line">ret = v1.list_service_for_all_namespaces(watch=<span class="literal">False</span>)</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> ret.items:</span><br><span class="line">    print(<span class="string">"%s \t%s \t%s \t%s \t%s \n"</span> % (i.kind, i.metadata.namespace, i.metadata.name, i.spec.cluster_ip, i.spec.ports ))</span><br><span class="line">    </span><br><span class="line"><span class="comment">#列出所有的pod</span></span><br><span class="line">ret = v1.list_pod_for_all_namespaces(watch=<span class="literal">False</span>)</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> ret.items:</span><br><span class="line">    print(<span class="string">"%s\t%s\t%s"</span> % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))</span><br><span class="line"></span><br><span class="line"><span class="comment">#列出所有deploy</span></span><br><span class="line">ret = v1.list_deployments_for_all_namespaces(watch=<span class="literal">False</span>)</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> ret.items:</span><br><span class="line">    print(<span class="string">"%s\t%s\t%s"</span> % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))</span><br><span class="line">    </span><br><span class="line"><span class="comment">##列出其他资源和以上类似，不懂可以查看(kubectl  api-resources)</span></span><br></pre></td></tr></table></figure>

<p>2、创建k8s资源对象</p>
<p>github:<a href="https://github.com/kubernetes-client/python/tree/master/examples" target="_blank" rel="noopener">https://github.com/kubernetes-client/python/tree/master/examples</a></p>
<p>创建资源（提前写好yaml资源清单）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#创建deploy</span></span><br><span class="line">[root@k8s-m ~]<span class="comment"># cat create_deploy.py</span></span><br><span class="line"><span class="keyword">from</span> os <span class="keyword">import</span> path</span><br><span class="line"><span class="keyword">import</span> yaml</span><br><span class="line"><span class="keyword">from</span> kubernetes <span class="keyword">import</span> client, config</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">main</span><span class="params">()</span>:</span></span><br><span class="line">    config.load_kube_config()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">with</span> open(path.join(path.dirname(__file__), <span class="string">"/root/deploy.yaml"</span>)) <span class="keyword">as</span> f:</span><br><span class="line">        dep = yaml.safe_load(f)</span><br><span class="line">        k8s_apps_v1 = client.AppsV1Api()</span><br><span class="line">        resp = k8s_apps_v1.create_namespaced_deployment(</span><br><span class="line">            body=dep, namespace=<span class="string">"default"</span>)</span><br><span class="line">        print(<span class="string">"Deployment created. status='%s'"</span> % resp.metadata.name)</span><br><span class="line">main()</span><br><span class="line"></span><br><span class="line">[root@k8s-m ~]<span class="comment"># kubectl get pod </span></span><br><span class="line">NAME                        READY   STATUS    RESTARTS   AGE</span><br><span class="line">mydeploy<span class="number">-6946</span>c867dc-bgcld   <span class="number">1</span>/<span class="number">1</span>     Running   <span class="number">0</span>          <span class="number">40</span>s</span><br><span class="line">mydeploy<span class="number">-6946</span>c867dc-rdnvj   <span class="number">1</span>/<span class="number">1</span>     Running   <span class="number">0</span>          <span class="number">40</span>s</span><br><span class="line">[root@k8s-m ~]<span class="comment"># kubectl get deploy</span></span><br><span class="line">NAME       READY   UP-TO-DATE   AVAILABLE   AGE</span><br><span class="line">mydeploy   <span class="number">2</span>/<span class="number">2</span>     <span class="number">2</span>            <span class="number">2</span>           <span class="number">44</span>s</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#创建pod例子（其它资源得自己查源码自己找对应的API）</span></span><br><span class="line">[root@k8s-m ~]<span class="comment"># cat create_pod.py </span></span><br><span class="line"><span class="keyword">from</span> os <span class="keyword">import</span> path</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> yaml</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> kubernetes <span class="keyword">import</span> client, config</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">main</span><span class="params">()</span>:</span></span><br><span class="line">    config.load_kube_config()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">with</span> open(path.join(path.dirname(__file__), <span class="string">"/root/pod.yaml"</span>)) <span class="keyword">as</span> f:</span><br><span class="line">        dep = yaml.safe_load(f)</span><br><span class="line">        k8s_core_v1 = client.CoreV1Api()</span><br><span class="line">        resp = k8s_core_v1.create_namespaced_pod(</span><br><span class="line">            body=dep, namespace=<span class="string">"default"</span>)</span><br><span class="line">        print(<span class="string">"Pod created. status='%s'"</span> % resp.metadata.name)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    main()</span><br><span class="line"></span><br><span class="line"><span class="comment">##</span></span><br><span class="line">[root@k8s-m ~]<span class="comment"># python3  create_pod.py </span></span><br><span class="line">Pod created. status=<span class="string">'nginx-pod'</span></span><br><span class="line">[root@k8s-m ~]<span class="comment"># kubectl get pod  nginx-pod</span></span><br><span class="line">NAME        READY   STATUS    RESTARTS   AGE</span><br><span class="line">nginx-pod   <span class="number">1</span>/<span class="number">1</span>     Running   <span class="number">0</span>          <span class="number">8</span>s</span><br></pre></td></tr></table></figure>

<p>3、删除资源（我这里展示pod例子，其它资源删除差不多）<br>参考地址:/usr/local/python3/lib/python3.6/site-packages/kubernetes/client/</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">[root@k8s-m ~]<span class="comment"># cat  dp.py</span></span><br><span class="line"><span class="keyword">from</span> os <span class="keyword">import</span> path</span><br><span class="line"><span class="keyword">import</span> yaml</span><br><span class="line"><span class="keyword">from</span> kubernetes <span class="keyword">import</span> client, config</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">main</span><span class="params">()</span>:</span></span><br><span class="line">    config.load_kube_config()</span><br><span class="line">    k8s_core_v1 = client.CoreV1Api()</span><br><span class="line">    resp = k8s_core_v1.delete_namespaced_pod(namespace=<span class="string">"default"</span>,name=<span class="string">'nginx-pod'</span>)</span><br><span class="line">    print(<span class="string">"delete Pod "</span>)</span><br><span class="line"></span><br><span class="line">[root@k8s-m ~]<span class="comment"># python3 dp.py</span></span><br><span class="line">delete Pod</span><br></pre></td></tr></table></figure>

<p>4、查看资源（类似kubectl get pod xxx -o json）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#查看(read)</span></span><br><span class="line"></span><br><span class="line">[root@k8s-m ~]<span class="comment"># cat  rp.py </span></span><br><span class="line"><span class="keyword">from</span> os <span class="keyword">import</span> path</span><br><span class="line"><span class="keyword">import</span> yaml</span><br><span class="line"><span class="keyword">from</span> kubernetes <span class="keyword">import</span> client, config</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">main</span><span class="params">()</span>:</span></span><br><span class="line">    config.load_kube_config()</span><br><span class="line">    k8s_core_v1 = client.CoreV1Api()</span><br><span class="line">    resp = k8s_core_v1.read_namespaced_pod(namespace=<span class="string">"default"</span>,name=<span class="string">'nginx-pod'</span>)</span><br><span class="line">    print(<span class="string">"read Pod "</span>)</span><br><span class="line">    <span class="comment">#详细信息</span></span><br><span class="line">    print(resp)</span><br><span class="line">    <span class="comment">#指定信息</span></span><br><span class="line">    print(resp.spec.containers[<span class="number">0</span>].image)</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    main()</span><br><span class="line"></span><br><span class="line">[root@k8s-m ~]<span class="comment"># python3   rp.py  |tail</span></span><br><span class="line">            <span class="string">'host_ip'</span>: <span class="string">'172.31.250.229'</span>,</span><br><span class="line">            <span class="string">'init_container_statuses'</span>: <span class="literal">None</span>,</span><br><span class="line">            <span class="string">'message'</span>: <span class="literal">None</span>,</span><br><span class="line">            <span class="string">'nominated_node_name'</span>: <span class="literal">None</span>,</span><br><span class="line">            <span class="string">'phase'</span>: <span class="string">'Running'</span>,</span><br><span class="line">            <span class="string">'pod_ip'</span>: <span class="string">'10.244.167.134'</span>,</span><br><span class="line">            <span class="string">'qos_class'</span>: <span class="string">'BestEffort'</span>,</span><br><span class="line">            <span class="string">'reason'</span>: <span class="literal">None</span>,</span><br><span class="line">            <span class="string">'start_time'</span>: datetime.datetime(<span class="number">2019</span>, <span class="number">8</span>, <span class="number">30</span>, <span class="number">9</span>, <span class="number">13</span>, <span class="number">49</span>, tzinfo=tzutc())&#125;&#125;</span><br><span class="line">nginx</span><br></pre></td></tr></table></figure>

<p>5、修改</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">[root@k8s-m ~]<span class="comment"># cat  pp.py </span></span><br><span class="line"><span class="keyword">from</span> os <span class="keyword">import</span> path</span><br><span class="line"><span class="keyword">import</span> yaml</span><br><span class="line"><span class="keyword">from</span> kubernetes <span class="keyword">import</span> client, config</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">main</span><span class="params">()</span>:</span></span><br><span class="line">    config.load_kube_config()</span><br><span class="line">    k8s_core_v1 = client.CoreV1Api()</span><br><span class="line">    old_resp = k8s_core_v1.read_namespaced_pod(namespace=<span class="string">"default"</span>,name=<span class="string">'nginx-pod'</span>)</span><br><span class="line">    old_resp.spec.containers[<span class="number">0</span>].image = <span class="string">"nginx:alpine"</span></span><br><span class="line">    <span class="comment">#修改镜像</span></span><br><span class="line">    new_resp = k8s_core_v1.patch_namespaced_pod(namespace=<span class="string">"default"</span>,name=<span class="string">'nginx-pod'</span>,body=old_resp)</span><br><span class="line">    print(new_resp.spec.containers[<span class="number">0</span>].image)</span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    main()</span><br><span class="line"></span><br><span class="line">[root@k8s-m ~]<span class="comment"># python3 pp.py </span></span><br><span class="line">nginx:alpine</span><br></pre></td></tr></table></figure>


    </div>

  </article>
  <div class="toc-container">
    

  </div>
</div>
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=5sr5du46f27&amp;m=0&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33" async="async"></script>
<div class="copyright">
    <span>本作品采用</span>
    <a href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>


  
    <div class="post-nav" style="margin-left:-168px;">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2020/08/17/破坏性测试/" rel="next" title="破坏性测试">
          破坏性测试
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2020/08/29/Go语言之结构体/" rel="prev" title="Go语言之结构体">
            Go语言之结构体
          </a>
          <span>〉</span>
        
      </div>
    </div>
  


	
	<div style="width:109%; margin-left:-144px" id="lv-container" data-id="city" data-uid="MTAyMC80OTg5NS8yNjM4Ng==">
	<script type="text/javascript">
   	   (function(d, s) {
       		var j, e = d.getElementsByTagName(s)[0];

       		if (typeof LivereTower === 'function') { return; }

       		j = d.createElement(s);
       		j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
       		j.async = true;

       		e.parentNode.insertBefore(j, e);
   	   })(document, 'script');
	</script>
	<noscript> 为正常使用来必力评论功能请激活JavaScript</noscript>
        </div>
	



    </div>

    

  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href target="_blank">GitHub</a> |
        <a class="bottom-item" href>友情链接</a> |
        <a class="bottom-item" href="https://hexo.io" target="_blank">Powered by hexo</a> |
        <a class="bottom-item" href="https://github.com/fooying/hexo-theme-xoxo-plus" target="_blank">Theme xoxo-plus</a> |
        <a class="bottom-item" href="/atom.xml">订阅</a>
    </div>
</footer>

  

<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



  

</body>
</html>
